import { requestGet, requestPost } from '@/api/system/common'

/**
 * 办事大厅设置
 */
export const officeSetting = {
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      jxx: [],
      dataList: [],
      dataSource: [],
      queryList: [],
      entityTemp: [],
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      openType: [
        { id: 1, value: 1, label: '地址参数' },
        { id: 2, value: 2, label: '地址传参' },
        { id: 3, value: 3, label: '详情模版' }
      ],
      form: {
        size: 1,
        row: 1,
        isOpenMore: false,
        gdxsx: false,
        xsnr: [],
        query: [],
        image: '',
        title: '',
        entity: '',
        url: '',
        id: '',
        isOpenEntity: false,
        entityTemplateId: null, // 详情页模版编号
        entityParams: [], // 详情参数设置
        params: [],
        entityOpenType: null, // 详情页打开方式
        entityUrl: '', // 详情地址
        entityTemp: null
      }
    }
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (newName.sjlx === 1) {
            this.initDataJxx()
          } else if (newName.sjlx === 2) {
            this.initSystemJxx()
          }
          if (
            undefined !== newName.pzx &&
            newName.pzx !== '' &&
            newName.pzx !== null
          ) {
            this.form = JSON.parse(newName.pzx)
            if (this.form.query === undefined) {
              this.form.query = []
            }
          }
          if (newName.id !== null) {
            this.initDataList()
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    document.body.ondrop = function(event) {
      event.preventDefault()
      event.stopPropagation()
    }
    this.initDataSource()
    this.initAllTemp()
    this.initQuery()
  },
  methods: {
    entityChange(data) {
      const entity = this.entityTemp.filter(item => {
        if (item.id === data) {
          return item
        }
      })
      if (entity !== undefined && entity !== null && entity.length > 0) {
        const param = entity[0].params
        this.form.entityTemp = entity[0]
        this.form.entityParams = []
        this.form.entityParams.push({
          name: '服务编号',
          code: 'id',
          valueType: 6,
          test: false,
          testValue: null
        })
        if (undefined !== param && param !== null) {
          const newParams = JSON.parse(param).map(item => {
            item.test = false
            item.value = ''
            return item
          })
          newParams.forEach(item => {
            this.form.entityParams.push(item)
          })
        } else {
          this.form.entityParams = []
          this.form.entityParams.push({
            name: '服务编号',
            code: 'id',
            valueType: 6,
            test: false,
            testValue: null
          })
        }
      }
    },
    initAllTemp() {
      requestGet('rest/api/temp/v1/entity/temp/blurry', {})
        .then(res => {
          const { data } = res
          this.entityTemp = data.data
        })
        .catch(() => {
          this.entityTemp = []
        })
    },
    removeParams(item) {
      const index = this.form.params.indexOf(item)
      if (index !== -1) {
        this.form.params.splice(index, 1)
      }
    },
    doAddParams() {
      const item = { code: '', column: '' }
      if (this.form.params === undefined) {
        this.form.params = []
      }
      this.form.params.push(item)
    },
    save() {
      this.loading = true
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form)
          requestPost('rest/api/temp/v1/column/manager/update/entity', this.data)
            .then(res => {
              this.close()
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          this.loading = false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    initDataJxx() {
      requestGet('rest/api/template/v1/analysis/data/source', { id: this.data.sjx }).then(res => {
        const { data } = res
        this.jxx = data
      })
    },
    initSystemJxx() {
      requestGet('rest/api/template/v1/analysis/data/system', { id: this.data.sjx }).then(res => {
        const { data } = res
        this.jxx = data
      })
    },
    initDataList() {
      requestGet('rest/api/temp/v1/column/analysis/sys', { cid: this.data.id, page: 1, size: 2000, obj: false })
        .then(res => {
          const { data } = res
          if (undefined !== data && data !== null) {
            this.dataList = data.data
          } else {
            this.dataList = []
          }
        })
        .catch(() => {
          this.dataList = []
        })
    },
    initDataSource() {
      requestGet('rest/config/api/data/layz', {}).then(res => {
        const { data } = res
        if (undefined !== data && data !== null) {
          this.dataSource = data.data
        }
      })
    },
    initQuery() {
      requestGet('rest/api/config/v1/enum/query/service/type', {})
        .then(res => {
          const { data } = res
          this.queryList = data
        })
        .catch(() => {
          this.queryList = []
        })
    },
    doAdd() {
      const item = { label: '', column: '', type: null, ysf: null, sjx: null }
      this.form.query.push(item)
    },
    toDelete(item) {
      const index = this.form.query.indexOf(item)
      if (index !== -1) {
        this.form.query.splice(index, 1)
      }
    },
    clone() {
      this.form.query = this.form.query.map((item, index) => {
        item.px = index + 1
        return item
      })
    }
  }
}

export const officeHall = {
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      pagerSize: 0,
      size: 0,
      query: {
        blurry: '',
        type: 1,
        hot: 1,
        code: '',
        zm: 1,
        pj: 1,
        sc: 1,
        params: null,
        page: 1,
        size: 10
      },
      dataList: [], // 服务数据
      column: [], // 配置信息
      queryList: [], // 查询条件列表
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      setting: [],
      activeHeader: {},
      params: []
    }
  },
  computed: {
    borderStyle() {
      if (this.temp.sfxsbk === true) {
        return {
          border: '1px solid #eeeeee',
          backgroundColor: this.layoutTheme.menuBackground,
          color: this.layoutTheme.menuColor,
          borderRadius: this.layoutTheme.borderRadius
        }
      }
      if (this.temp.bkyy === true) {
        return {
          boxShadow: '0 2px 4px rgba(0, 0, 0, .12)',
          backgroundColor: this.layoutTheme.menuBackground,
          color: this.layoutTheme.menuColor,
          borderRadius: this.layoutTheme.borderRadius
        }
      }
      return {
        backgroundColor: this.layoutTheme.menuBackground,
        color: this.layoutTheme.menuColor,
        borderRadius: this.layoutTheme.borderRadius
      }
    },
    customBorderStyle() {
      return {
        '--border-color': this.layoutTheme.menuBorderColor,
        '--font-color': this.layoutTheme.menuColor,
        '--font-assets-color': this.layoutTheme.assistColor,
        '--active-color': this.layoutTheme.dialogActiveColor
      }
    }
  },
  created() {
    this.initAllCountService()
  },
  watch: {
    temp: {
      handler(newData, oldData) {
        this.query = {
          blurry: ''
        }
        if (this.showHeader) {
          this.setting = newData.data.pzx !== '' ? JSON.parse(newData.data.pzx) : []
        } else {
          this.setting = JSON.parse(newData.pzx)
        }
        if (undefined !== this.setting.query && this.setting.query !== null) {
          this.queryList = this.setting.query
          if (this.queryList !== null && this.queryList.length > 0) {
            // 默认展示第一个选项大类
            this.headerClick(this.queryList[0])
          }
        } else {
          this.queryList = []
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    // 查询服务数据
    initData() {
      requestGet('rest/api/query/v2/service/sub/pager', this.query)
        .then(res => {
          const { data } = res
          this.dataList = data.data
          this.pagerSize = data.pageSize
        }).catch(() => {
          console.log('--------')
        })
    },
    // 一级分类点击事件
    headerClick(data) {
      this.activeHeader = data
      this.query.code = data.column
      this.initAllParams(data.column)
    },
    // 查询分类子项
    initAllParams(data) {
      requestGet('rest/api/config/v1/enum/query/service/params', { code: data })
        .then(res => {
          const { data } = res
          this.params = data
          this.itemClick(null)
        }).catch(() => {
          this.params = []
        })
    },
    // 查询全部可用服务
    initAllCountService() {
      requestGet('rest/config/api/service/all/count', {})
        .then(res => {
          const { data } = res
          this.size = data
        }).catch(() => {
          this.size = 0
        })
    },
    itemClick(data) {
      this.query.page = 1
      this.$set(this.query, 'params', data)
      // this.query.params = data
      this.initData()
    },
    queryObject() {
      this.query.page = 1
      this.query.size = 10
      this.initData()
    },
    changerAsc() {
      if (this.query.hot === 1) {
        this.$set(this.query, 'hot', 2)
        this.$set(this.query, 'sc', null)
        this.$set(this.query, 'pj', null)
        this.$set(this.query, 'zm', null)
      
      } else {
        this.$set(this.query, 'hot', 1)
        this.$set(this.query, 'sc', null)
        this.$set(this.query, 'pj', null)
        this.$set(this.query, 'zm', null)
      }
      this.query.page = 1
      this.initData()
    },
    changerSc() {
      if (this.query.sc === 1) {
        this.$set(this.query, 'sc', 2)
        this.$set(this.query, 'hot', null)
        this.$set(this.query, 'pj', null)
        this.$set(this.query, 'zm', null)
      } else {
        this.$set(this.query, 'sc', 1)
        this.$set(this.query, 'hot', null)
        this.$set(this.query, 'pj', null)
        this.$set(this.query, 'zm', null)
      }
      this.query.page = 1
      this.initData()
    },
    changerpj() {
      if (this.query.pj === 1) {
        this.$set(this.query, 'pj', 2)
        this.$set(this.query, 'hot', null)
        this.$set(this.query, 'sc', null)
        this.$set(this.query, 'zm', null)
      } else {
        this.$set(this.query, 'pj', 1)
        this.$set(this.query, 'hot', null)
        this.$set(this.query, 'sc', null)
        this.$set(this.query, 'zm', null)
      }
      this.query.page = 1
      this.initData()
    },
    changerZm() {
      if (this.query.zm === 1) {
        this.$set(this.query, 'zm', 2)
        this.$set(this.query, 'hot', null)
        this.$set(this.query, 'sc', null)
        this.$set(this.query, 'pj', null)
      } else {
        this.$set(this.query, 'zm', 1)
        this.$set(this.query, 'hot', null)
        this.$set(this.query, 'sc', null)
        this.$set(this.query, 'pj', null)
      }
      this.query.page = 1
      this.initData()
    },
    scfw(data) {
      // 收藏服务
      requestPost('rest/api/service/entity/collection', { serviceId: data.id }).then(res => {
        if (res.state === 200) {
          this.entity.sfsc = true
          this.$notify({
            title: '收藏成功',
            message: '服务收藏成功！',
            type: 'success'
          })
        } else {
          this.$notify({
            title: '收藏失败！',
            message: res.msg,
            type: 'warning'
          })
        }
      }).finally(() => {
        this.initData()
      })
    },
    openEntity(data) {
      window.open(data.openUrl, '_blank')
    },
    openFaq(data) {
      let title = ''
      if (this.setting.isFixTitle) {
        title = this.setting.entityTitle
      } else {
        title = data.name
      }
      const entity = {
        isOpenEntity: this.setting.isOpenEntity, // 是否打开详情页
        entityParams: this.setting.entityParams, // 详情页查询参数
        params: this.setting.params, // 详情页打开方式为地址参数时，参数设置
        openType: this.setting.entityOpenType, // 详情页打开方式
        url: this.setting.entityUrl, // 详情地址
        temp: this.setting.entityTemp, // 详情模版
        entity: data, // 详情数据
        title: title // 标题
      }
      this.$bus.emit('openEntity', { data: entity })
    }
  }
}
